const selectList_max_num=9 //9 等于 +4个搜索框



//点击+号
$(document).on('click','.add-btn',function (e) {  
    const selectList_length = $(".main-item-view").find(".centerview").length
    if(selectList_length<selectList_max_num){ 
        console.log($(".main-item-view").children().length)
        $(".main-item-view").append(addstr([1],$(".main-item-view").children().length))   
    }   
}) 
//点击-号
$(document).on('click','.del-btn',function (e) {    
    $(this).parents(".centerview").prev().remove()
    $(this).parents(".centerview").remove()
}) 


//搜索类型选择  
$("body").delegate(".select-view select","change", function(e){ 
    let changeIndex=Number($(this).children("option:selected").attr('data-index'))
    let eleDataName = $(this).children("option:selected").attr('data-name')
    let prevObject = centerview_get_index(changeIndex)[0] || centerview_get_index(changeIndex).prevObject[0]
    $(prevObject).find(".select-view span").text(eleDataName)
}); 
function centerview_get_index(index){
    let mainitem=$(`.main-item-view`).children(".centerview") 
    return mainitem.map((item)=>{
        if( Number($(mainitem[item]).attr("data-index")) == index){ 
            return $(mainitem[item])
        }
    })
} 
 

function addstr(list,num){
    let str = "" 
    list.map((item, index) => { 
        str += `

        <div class="centerview" data-index="${num+1}">
            <div class="sousuo">
                <div class="sousuo-left">
                    <div class="select-view">
                        <span>与</span>
                        <img src="images/dsj-icon.png"> 
                        <select>
                            <option value="1" data-index="${num+1}" data-name="与" selected="selected">与</option>
                            <option value="2" data-index="${num+1}" data-name="并">并</option>
                        </select>
                    </div>
                </div>
                <div class="ele-btn" style="width: 0;border:none;"></div> 
            </div>
        </div>


        <div class="centerview" data-index="${num+2}">
            <div class="sousuo">
                <div class="sousuo-left">
                    <div class="select-view">
                        <span>全部</span>
                        <img src="images/dsj-icon.png"> 
                        <select>
                            <option value="1" data-index="${num+2}" data-name="全部" selected="selected">全部</option>
                            <option value="2" data-index="${num+2}" data-name="题名">题名</option>
                            <option value="3" data-index="${num+2}" data-name="版本">版本</option>
                            <option value="4" data-index="${num+2}" data-name="民族">民族</option>
                            <option value="5" data-index="${num+2}" data-name="省市">省市</option>
                        </select>  
                    </div>
                    <input type="text">
                </div>
                <div class="ele-btn" style="width: 0;border:none;"></div>
                <div class="ele-btn add-btn">+</div>
                <div class="ele-btn del-btn">-</div>
            </div>
        </div>


        ` 
    })
    return str
}

